package de.vocabulary.server.persistence;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import de.vocabulary.server.persistence.BerkeleyDbDatabaseSerialBinding;
import de.vocabulary.server.persistence.BerkeleyDbEnvironment;
import de.vocabulary.shared.model.Vocable;


public class DBTestSerialBinding {
    

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();
    
	@Test
	public void dbTest() throws Exception {
		
		BerkeleyDbEnvironment dbEnv = new BerkeleyDbEnvironment(folder.newFolder().getAbsolutePath(), false, true);
		BerkeleyDbDatabaseSerialBinding db = new BerkeleyDbDatabaseSerialBinding(
				dbEnv, Vocable.class
		);
		
		db.put("1", new Vocable("house", "Haus"));
		db.put("2", new Vocable("mouse", "Maus"));
		
		db.close();
		
		BerkeleyDbDatabaseSerialBinding dbRead = new BerkeleyDbDatabaseSerialBinding(
				dbEnv, Vocable.class
		);
		
		for (String key : dbRead.getKeys()) {
			System.out.println(key + "- " + (Vocable) dbRead.get(key));
		}
		
		assertTrue(dbRead.hasKey("1"));
		assertFalse(dbRead.hasKey("3"));
		
		System.out.println(dbRead.getContentInformation());
		
		dbRead.close();		
		dbEnv.close();
		
	}
}
